home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / time / sunclock / source / astro.c next >
C/C++ Source or Header  |  1996-04-07  |  4KB  |  166 lines

  1. /*
  2.  * Sun clock - astronomical routines.
  3.  */
  4. #include <time.h>
  5. #include <math.h>
  6. #include "sunmath.h"
  7.  
  8. /*  JDATE  --  Convert internal GMT date and time to Julian day
  9.            and fraction.  */
  10.  
  11. long
  12. jdate(t)
  13. struct tm *t;
  14. {
  15.     long c, m, y;
  16.  
  17.     y = t->tm_year + 1900;
  18.     m = t->tm_mon + 1;
  19.     if (m > 2)
  20.        m = m - 3;
  21.     else {
  22.        m = m + 9;
  23.        y--;
  24.     }
  25.     c = y / 100L;           /* Compute century */
  26.     y -= 100L * c;
  27.     return t->tm_mday + (c * 146097L) / 4 + (y * 1461L) / 4 +
  28.         (m * 153L + 2) / 5 + 1721119L;
  29. }
  30.  
  31. /* JTIME --    Convert internal GMT  date  and    time  to  astronomical
  32.            Julian  time  (i.e.   Julian  date  plus  day fraction,
  33.            expressed as a double).    */
  34.  
  35. double
  36. jtime(t)
  37. struct tm *t;
  38. {
  39.     return (jdate(t) - 0.5) + 
  40.        (((long) t->tm_sec) +
  41.          60L * (t->tm_min + 60L * t->tm_hour)) / 86400.0;
  42. }
  43.  
  44. /*  KEPLER  --    Solve the equation of Kepler.  */
  45.  
  46. double
  47. kepler(m, ecc)
  48. double m, ecc;
  49. {
  50.     double e, delta,lastdelta = 0;
  51. #define EPSILON 1E-6
  52.     e = m = dtr(m);
  53.     do {
  54.        delta = e - ecc * sin(e) - m;
  55.        e -= delta / (1 - ecc * cos(e));
  56.        if (delta == lastdelta) return(e);
  57.        lastdelta = delta;
  58.     } while (fabs(delta) > EPSILON);
  59.     return e;
  60. }
  61.  
  62. /*  SUNPOS  --    Calculate position of the Sun.    JD is the Julian  date
  63.         of  the  instant for which the position is desired and
  64.         APPARENT should be nonzero if  the  apparent  position
  65.         (corrected  for  nutation  and aberration) is desired.
  66.                 The Sun's co-ordinates are returned  in  RA  and  DEC,
  67.         both  specified  in degrees (divide RA by 15 to obtain
  68.         hours).  The radius vector to the Sun in  astronomical
  69.                 units  is returned in RV and the Sun's longitude (true
  70.         or apparent, as desired) is  returned  as  degrees  in
  71.         SLONG.    */
  72.  
  73. sunpos(double jd, int apparent, 
  74.         double *ra, double *dec, double *rv, double *slong)
  75. {
  76.     double t, t2, t3, a, l, m, e, ea, v, theta, omega, eps;
  77.  
  78.     /* Time, in Julian centuries of 36525 ephemeris days,
  79.        measured from the epoch 1900 January 0.5 ET. */
  80.  
  81.     t = (jd - 2415020.0) / 36525.0;
  82.     t2 = t * t;
  83.     t3 = t2 * t;
  84.  
  85.     /* Geometric mean longitude of the Sun, referred to the
  86.        mean equinox of the date. */
  87.  
  88.     a = 279.69668 + 36000.76892 * t + 0.0003025 * t2;
  89.     l = fixangle(a);
  90.  
  91.         /* Sun's mean anomaly. */
  92.  
  93.     a = 358.47583 + 35999.04975*t - 0.000150*t2 - 0.0000033*t3;
  94.     m = fixangle(a);
  95.  
  96.         /* Eccentricity of the Earth's orbit. */
  97.  
  98.     e = 0.01675104 - 0.0000418 * t - 0.000000126 * t2;
  99.  
  100.     /* Eccentric anomaly. */
  101.  
  102.     ea = kepler(m, e);
  103.  
  104.     /* True anomaly */
  105.  
  106.     a = atan(sqrt((1 + e) / (1 - e))  * tan(ea / 2));
  107.     a = 2 * rtd(a);
  108.     v = fixangle(a);
  109.  
  110.     /* Sun's true longitude. */
  111.  
  112.     theta = l + v - m;
  113.  
  114.     /* Obliquity of the ecliptic. */
  115.  
  116.     eps = 23.452294 - 0.0130125 * t - 0.00000164 * t2 + 0.000000503 * t3;
  117.  
  118.         /* Corrections for Sun's apparent longitude, if desired. */
  119.  
  120.     if (apparent) {
  121.        a = 259.18 - 1934.142 * t;
  122.        omega = fixangle(a);
  123.        theta = theta - 0.00569 - 0.00479 * sin(dtr(omega));
  124.        eps += 0.00256 * cos(dtr(omega));
  125.     }
  126.  
  127.         /* Return Sun's longitude and radius vector */
  128.  
  129.     *slong = theta;
  130.     *rv = (1.0000002 * (1 - e * e)) / (1 + e * cos(dtr(v)));
  131.  
  132.     /* Determine solar co-ordinates. */
  133.  
  134.     a = atan2(cos(dtr(eps)) * sin(dtr(theta)), cos(dtr(theta)));
  135.     a = rtd(a);
  136.     *ra = fixangle(a);
  137.     a = asin(sin(dtr(eps)) * sin(dtr(theta)));
  138.     *dec = rtd(a);
  139. }
  140.  
  141. /*  GMST  --  Calculate Greenwich Mean Siderial Time for a given
  142.           instant expressed as a Julian date and fraction.    */
  143.  
  144. double
  145. gmst(jd)
  146. double jd;
  147. {
  148.     double t, theta0;
  149.  
  150.  
  151.     /* Time, in Julian centuries of 36525 ephemeris days,
  152.        measured from the epoch 1900 January 0.5 ET. */
  153.  
  154.     t = ((floor(jd + 0.5) - 0.5) - 2415020.0) / 36525.0;
  155.  
  156.     theta0 = 6.6460656 + 2400.051262 * t + 0.00002581 * t * t;
  157.  
  158.     t = (jd + 0.5) - (floor(jd + 0.5));
  159.  
  160.     theta0 += (t * 24.0) * 1.002737908;
  161.  
  162.     theta0 = (theta0 - 24.0 * (floor(theta0 / 24.0)));
  163.  
  164.     return theta0;
  165. }
  166.